package yxy.game.pm2.active.command.mrsc;

import com.alibaba.fastjson.JSONObject;
import yxy.apple.logger.Logger;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_每日首充;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ActiveMessage;
import yxy.game.pm2.message.ActiveMessage.ActMrscInfoRequest;

import java.util.ArrayList;

/**
 * 每日首充活动信息
 * @author Dell
 *///0x0240010A
@MessageType(proto = Command.ActMrscInfo, classtype = ActiveMessage.ActMrscInfoRequest.class)
public class CMD_Active_每日首充_Info extends Command<ActMrscInfoRequest> {

	private static final Logger logger = Logger.create(CMD_Active_每日首充_Info.class);
	
	@Override
	public void execute(MessageSource messageSource, ActMrscInfoRequest request) throws Exception {
		String pcode = messageSource.getTag();
		ActiveMessage.ActMrscInfoResponse.Builder response = ActiveMessage.ActMrscInfoResponse.newBuilder();
		
		ActiveModule_每日首充 am = ActiveModule_每日首充.get(pcode,false);
		
		if(am == null) {
			response.setStarttime(0);
			response.setEndtime(0);
			response.setTopupexp(0);
			send(messageSource, response, Result.ActNoOpen);
			return;
		}
		
		if(!am.running()) {
			response.setStarttime(0);
			response.setEndtime(0);
			response.setTopupexp(0);
			send(messageSource, response, Result.ActNoOpen);
			return;
		}
			
		response.setStarttime(am.getTimeline().getStartTime().getMillis());
		response.setEndtime(am.getTimeline().getEndTime().getMillis());
		response.setTopupexp(am.getPayexp());
		
		JSONObject json = am.getNeedtopupexp();
		for(String key : json.keySet()) {
			ActiveMessage.NeedTopUpExp.Builder ntue = ActiveMessage.NeedTopUpExp.newBuilder();
			ntue.setId(Integer.valueOf(key));
			ntue.setNeedexp(json.getIntValue(key));
			response.addNeedtopupexp(ntue);
		}
		
		ArrayList<Integer> cangetids = am.getCangetidlist(); //可以领取的奖励 id
		for(int i = 0; i < cangetids.size(); i++) {
			response.addCangetid(cangetids.get(i));
		}
		
		ArrayList<Integer> getids = am.getGetidlist(); //已领取的奖励 id
		for(int i = 0; i < getids.size(); i++) {
			response.addGetid(getids.get(i));
		}
		
		send(messageSource, response, Result.Success);
	}

	private void send(MessageSource messageSource, ActiveMessage.ActMrscInfoResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}
